﻿using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using Akso.DataExporting.Excel.EpPlus;
using Akso.Dto;
using Akso.Vendors.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Akso.Vendors.Exporting
{
    public class VendorListExcelExporter : EpPlusExcelExporterBase, IVendorListExcelExporter
    {
        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;

        public VendorListExcelExporter(
            ITimeZoneConverter timeZoneConverter,
            IAbpSession abpSession)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }

        public FileDto ExportToFile(List<ExportVendorDto> vendorListDtos)
        {
            return CreateExcelPackage(
                _abpSession.UserId + "_" + DateTime.Now.ToString() + "_VendorList.xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.Workbook.Worksheets.Add(L("Page_BasicData.Vendors_Head"));
                    sheet.OutLineApplyStyle = true;

                    AddHeader(
                        sheet,
                        L("Page_BasicData.Vendors_Column_Name"),
                        L("Page_BasicData.Vendors_Column_Address"),
                        L("Page_BasicData.Vendors_Column_Contact"),
                        L("Page_BasicData.Vendors_Column_ContactMobile"),
                        L("Page_BasicData.Vendors_Column_ContactTelephone"),
                        L("Page_BasicData.Vendors_Column_CreatorUserId"),
                        L("Page_BasicData.Vendors_Column_CreationTime"),
                        L("Page_BasicData.Vendors_Column_LastModifierUserId"),
                        L("Page_BasicData.Vendors_Column_LastModificationTime")
                        );

                    AddObjects(
                        sheet, 2, vendorListDtos,
                        _ => _.Name,
                        _ => _.Address,
                        _ => _.Contact,
                        _ => _.ContactMobile,
                        _ => _.ContactTelephone,
                        _ => _.CreatorUserId,
                        _ => _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId()),
                        _ => _.LastModifierUserId,
                        _ => _timeZoneConverter.Convert(_.LastModificationTime, _abpSession.TenantId, _abpSession.GetUserId())
                        );

                    //Formatting cells

                    var lastLoginTimeColumn = sheet.Column(7);
                    lastLoginTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd";

                    var creationTimeColumn = sheet.Column(9);
                    creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd";

                    for (var i = 1; i <= 7; i++)
                    {
                        sheet.Column(i).AutoFit();
                    }
                });
        }
    }
}
